package com.yuelan.codelib.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;

import android.os.Environment;
import android.os.StatFs;

/**
 * class name:SDCardUtil<BR>
 * class description:sd卡工具类<BR>
 * PS�?<BR>
 * 
 * @version 1.00 2012-5-3
 * @author ZHENSHI)peijiangping
 */
public class SDCardUtil {

	/**
	 * Role:在sd卡中创建文件�?BR> Date:2012-5-3<BR>
	 * 
	 * @author ZHENSHI)peijiangping
	 */
	public static File createFolder(String path) {
		File tmpFile = new File(path);
		// // LogUtil.v(path + "path");
		if (!tmpFile.exists()) {
			tmpFile.mkdirs();
			// // LogUtil.v(tmpFile.mkdirs() + "tmpFile.mkdirs()");
		}
		return tmpFile;
	}

	/**
	 * 作用：删除文件或者文件夹mode=1全部删除，mode=2保留最外面的文件夹<BR>
	 * 作者：peijiangping<BR>
	 * 日期：2012-6-28
	 */
	public static boolean deleteSDCardFile(File file, int mode) {
		if (file.exists()) { // 判断文件是否存在
			if (file.isFile()) { // 判断是否是文件
				file.delete(); // delete()方法 你应该知道 是删除的意思;
			} else if (file.isDirectory()) { // 否则如果它是一个目录
				File files[] = file.listFiles(); // 声明目录下所有的文件 files[];
				for (int i = 0; i < files.length; i++) { // 遍历目录下所有的文件
					deleteSDCardFile(files[i], 1); // 把每个文件 用这个方法进行迭代
				}
				if (mode == 1) {
					file.delete();
				}
			}
		} else {
			return false;
		}
		return true;
	}

	/**
	 * Role:获得sd卡根目录<BR>
	 * Date:2012-5-3<BR>
	 * 
	 * @author ZHENSHI)peijiangping
	 */
	public static String getSDCardPath() {
		String path = Environment.getExternalStorageDirectory()
				.getAbsolutePath();
		return path;
	}

	public static String getExtSDCardPath() {
		String sdcard_path = null;
		String sd_default = Environment.getExternalStorageDirectory()
				.getAbsolutePath();
		if (sd_default.endsWith("/")) {
			sd_default = sd_default.substring(0, sd_default.length() - 1);
		}
		try {
			Runtime runtime = Runtime.getRuntime();
			Process proc = runtime.exec("mount");
			InputStream is = proc.getInputStream();
			InputStreamReader isr = new InputStreamReader(is);
			String line;
			BufferedReader br = new BufferedReader(isr);
			while ((line = br.readLine()) != null) {
				if (line.contains("secure"))
					continue;
				if (line.contains("asec"))
					continue;
				if (line.contains("fat") && line.contains("/mnt/")) {
					String columns[] = line.split(" ");
					if (columns != null && columns.length > 1) {
						if (sd_default.trim().equals(columns[1].trim())) {
							continue;
						}
						sdcard_path = columns[1];
					}
				} else if (line.contains("fuse") && line.contains("/mnt/")) {
					String columns[] = line.split(" ");
					if (columns != null && columns.length > 1) {
						if (sd_default.trim().equals(columns[1].trim())) {
							continue;
						}
						sdcard_path = columns[1];
					}
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return sdcard_path;
	}

	/**
	 * Role:判断sd卡是否已插入<BR>
	 * Date:2012-5-3<BR>
	 * 
	 * @author ZHENSHI)peijiangping
	 */
	public static boolean hasStorage() {
		String state = android.os.Environment.getExternalStorageState();
		if (android.os.Environment.MEDIA_MOUNTED.equals(state)) {
			return true;
		}
		return false;
	}

	public static boolean canUserSDCard(long size) {
		String state = android.os.Environment.getExternalStorageState();
		if (android.os.Environment.MEDIA_MOUNTED.equals(state)) {
			String path = Environment.getExternalStorageDirectory()
					.getAbsolutePath();
			StatFs sf = new StatFs(path);
			long blockSize = sf.getBlockSize();
			long availCount = sf.getAvailableBlocks();
			long avsize = blockSize * availCount;
//			LOG.v(avsize + "avsize");
			if (avsize < size) {
				return false;
			}
			return true;
		}
		return false;
	}

	/**
	 * 作用：将buffer写入文件<BR>
	 * 作�?：peijiangping<BR>
	 * 日期�?012-6-28
	 */
	public static void writeSDCardFile(String path, byte[] buffer) {
		File file = new File(path);
		try {
			FileOutputStream fos = new FileOutputStream(file);
			fos.write(buffer);
			fos.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
